﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//编写程序实现一个计算器，包括十个数字（0~9）按钮和四
//个运算符（加、减、乘、除）按钮，以及等号和清空两个
//辅助按钮，还有一个显示输入输出的文本框，显示表达式
//及结果如：18+5=23

namespace 第五次作业
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //点击按钮后，文本框输入1
            TextBox_result.Text += "1";
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "4";
        }
        private void button5_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "9";
        }

        private void button0_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "0";
        }

        private void button_plus_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "+";
        }

        private void button_sub_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "-";
        }

        private void button_mul_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "*";
        }

        private void button_div_Click(object sender, EventArgs e)
        {
            TextBox_result.Text += "/";
        }

        private void button_equal_Click(object sender, EventArgs e)
        {
            //用DataTable的Compute方法计算表达式的结果
            var result = new System.Data.DataTable().Compute(TextBox_result.Text, null);
            TextBox_result.Text += "="+result;
        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            TextBox_result.Text = "";
        }

        private void button_point_Click(object sender, EventArgs e)
        {
           
            //没有数字输入时，不能输入小数点
            if(TextBox_result.Text == "")
            {
                MessageBox.Show("请输入数字后再输入小数点！");
                return;
            }

            char lastChar = TextBox_result.Text[TextBox_result.Text.Length - 1];
            if (lastChar == '+' || lastChar == '-' || lastChar == '*' || lastChar == '/')
            {
                MessageBox.Show("不能在运算符后直接输入小数点！");
                return;
            }

            string currentNumber = TextBox_result.Text.Split
                (new char[] { '+', '-', '*', '/' }).Last(); // 按运算符分割,获得当前输入的数字

            // 如果当前数字部分已包含小数点，则不允许再次输入小数点
            if (currentNumber.Contains("."))
            {
                MessageBox.Show("当前数字部分已经有小数点！");
                return;
            }

            //如果没有错误，添加小数点
            TextBox_result.Text += ".";
            
        }
    }
}
